/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.blog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.blog.idao.ICommentDAO;
import org.blog.model.Comment;

/**
 *
 * @author Training
 */
public class CommentDAO implements ICommentDAO {

    private Connection connection;

    public CommentDAO(Connection connection) {
        this.connection = connection;
    }

    public int insert(Comment comment) throws SQLException {
        String sql = "insert into comment(writer,content,article_id) values(?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, comment.getWriter());
        ps.setString(2, comment.getContent());
        ps.setInt(3, comment.getArticle().getId());

        return ps.executeUpdate();
    }

    public int update(int id, Comment commentUpdate) throws SQLException {
        String sql = "update comment set writer=?, content=?, article_id=? "
                + "where comment_id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps = this.connection.prepareStatement(sql);
        ps.setString(1, commentUpdate.getWriter());
        ps.setString(2, commentUpdate.getContent());
        ps.setInt(3, commentUpdate.getArticle().getId());
        ps.setInt(4, id);

        return ps.executeUpdate();
    }

    public int delete(int id) throws SQLException {
        String sql = "delete from comment where comment_id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps = this.connection.prepareStatement(sql);
        ps.setInt(1, id);

        return ps.executeUpdate();
    }

    public Comment getById(int id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Comment> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Comment> getByWriterLike(String writer) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Comment> getByContentLike(String content) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Comment> getByTitleLike(String title) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
